#基础
s = "Hello, Python world!"
year =2021
lst = ['Hello', 'Python', 'World!']
num_lst = [1,2,3]
insert_pos = 5
print(s[:insert_pos] + 'amazing ' + s[insert_pos])
print(s.replace('o', 'O', 1))
o_posiotions = [i for i, char in enumerate(s) if char == 'o']
print(o_posiotions)
print('-'.join(map(str,num_lst)))
print(s.islower())
print(s.isupper())
print('格式化后的字符串: ', 'Number: %d' % 100)
print(''.join(lst))
print(s.isdigit())
print(sorted(list(s)))
for char in s:
    print(char, end=' ')
print()
print(s.count(' '))
print(s.rfind('e'))
print(s.find('a'))
print(f"f-string合并: {s} {year}")
print('Hello'+"World!")
print(s.replace('Hello,' , " "))
print(s[7:13])
print(s[::-1])
print(s.strip('!'))
print(s.split(','))
print(s.endswith('!'))
print(s.startswith('Hello'))
print(s.replace('world','universe'))
print(s.find('Python'))
print(s.lower())
print(s.upper())
print(len(s))
print(s.count('o'))


#进阶
s = "Python is great!"
def reverse_string(s):
    reversed_str = ''
    for i in range(len(s)-1, -1, -1):
        reversed_str += s[i]
    return  reversed_str
print(reverse_string(s))

count_t = sum(1 for char in s if char == 't')
print(count_t)

print(s.replace('s','-'))
s2 = "A man, a plan, a canal, Panama!"
def is_padrome(s2):
    cleaned = ''.join(e.lower() for e in s if e.isalnum())
    return cleaned == cleaned[::-1]
    print(is_padrome(s2))

print(s.find('is'))
first_t = s.find('t')
if first_t != -1:
    print("插入后的字符串: ", s[:first_t+1] + '_inserted' + s[first_t+1])
print(s.replace('great','awesome'))

half = len(s) // 2
first_half = s[:half]
second_half = s[half:]
print(second_half + first_half)

word_list = [word.capitalize()for word in s.split()]
print(word_list)

def encrypt(s3, key_dict):
    return ''.join(key_dict.get(char, char)for char in s3)
encrypt_dict = {chr(ord('a')+i): chr(ord('m')+i) for i in range(14)}
encrypt_dict.update({chr(ord('o')+i): chr(ord('a')+i) for i in range(12)})
s3= "encryption"
print("加密后的字符串: ", encrypt(s3, encrypt_dict))